package com.odais.AE_Handler;

import java.util.ArrayList;

import org.andengine.engine.camera.ZoomCamera;
import org.andengine.entity.Entity;

import android.util.Log;

import com.odais.AE_Planner.People;
import com.odais.AE_Planner.RoomManager;
import com.odais.AE_Planner.RoomPlanner;
import com.odais.AE_Planner.SceneContext;
import com.odais.AE_Planner.Table;
import com.odais.DataManagement.DataManager;
import com.odais.Reservation.Reservation;
import com.odais.Reservation.ReservationBox;
import com.odais.Reservation.ReservationEntity;
import com.odais.Reservation.ReservationListModifierManager;



/**
 * 
 * @author Lionel DECELLE
 * @since 13 juil. 2012 - 08:44:24
 * 
 * @parameters TouchHandler takes : EnginePopulation (all things that stand in every scene : X lists of Y entities)  
 * 									EngineContext (defining the different engine's scenes)
 * 									EngineAction (supertype of action containing Event, Parameters, and concerned scene)
 * 
 * All engine related needs to be static.
 */
public class TouchHandler
{
// ===========================================================
// Constants
// ===========================================================
	private static final int SCENE_OFFSET = 150;

// ===========================================================
// Fields
// ===========================================================
	
	private EngineContext engineContext;
	private EngineAction engineAction, enginePreviousAction;
	private TouchEntityFinder touchEntityFinder;
	private Entity entity;
	private ReservationEntity reservationEntity;
	private ZoomCamera roomCamera;

	private ReservationListModifierManager reservationListModifierManager;
	private RoomManager roomManager;

	//Move behaviour
	private boolean entityMoved;
	private boolean cameraMoved;
	
	private float scaledX, scaledY, scaledXSceneB, scaledYSceneB;
	
	private int upDownCounter = 0;

// ===========================================================
// Constructors
// ===========================================================

	public TouchHandler()
	{
		touchEntityFinder = new TouchEntityFinder();
		enginePreviousAction = new EngineAction();
		reservationListModifierManager = null;
		roomManager = null;
	}
	
	public TouchHandler(EnginePopulation enginePopulation)
	{
		touchEntityFinder = new TouchEntityFinder();
		//this.enginePopulation = enginePopulation;
		enginePreviousAction = new EngineAction();
		reservationListModifierManager = null;
		roomManager = null;
	}
	
	public TouchHandler(EnginePopulation enginePopulation, EngineContext engineContext)
	{
		touchEntityFinder = new TouchEntityFinder();
		//this.enginePopulation = enginePopulation;
		this.engineContext = engineContext;
		enginePreviousAction = new EngineAction();
		reservationListModifierManager = null;
		roomManager = null;
	}

// ===========================================================
// Getter & Setter
// ===========================================================

	public void setCamera(ZoomCamera zoomCamera)
	{
		this.roomCamera = zoomCamera;
	}
	
	public void setReservationListModifierManager(
			ReservationListModifierManager reservationListModifierManager) {
		this.reservationListModifierManager = reservationListModifierManager;
	}
	
	public void setRoomManager(
			RoomManager roomManager) {
		this.roomManager = roomManager;
	}

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
	public void ProcessAction(EngineAction engineAction)
	{		
		this.engineAction = engineAction;
		
		switch(this.engineAction.getAction())
		{
			case ACTION_DOWN : 
					upDownCounter++;
					
					//Log.e("Odais", "action down");
					if(this.entity == null)
					{
						this.entity = touchEntityFinder.Find(this.engineAction.getPosition());
						this.enginePreviousAction.setAction(Action.ACTION_DOWN);
					}
					
					//modifier for dropped ReservationBox
					if((this.entity != null) & (upDownCounter<2))
					{
						if(this.engineAction.getSceneContext().getScene().getUserData()=="Room")
						{
							if(this.entity.getUserData()==EnginePopulation.ENGINE_POPULATION_RESERVATIONENTITY)
							{
								reservationEntity = (ReservationEntity) this.entity;
								reservationEntity.getReservationBox_RightPanel().setGrabbed(true);
								reservationEntity.getReservationBox_RightPanel().animate(this.engineAction.getSceneContext(), 0, 0, reservationEntity.getReservationBox_RightPanel().getX(), reservationEntity.getReservationBox_RightPanel().getY(), 1, 1/this.roomCamera.getZoomFactor(), 0);
							}					
						}
						else if(this.engineAction.getSceneContext().getScene().getUserData()=="Info")
						{
							if(this.entity.getUserData()==EnginePopulation.ENGINE_POPULATION_RESERVATIONENTITY)
							{
								reservationEntity = (ReservationEntity) this.entity;
								reservationEntity.getReservationBox_LeftPanel().setGrabbed(true);
								if(reservationListModifierManager!=null)
									reservationListModifierManager.setmRefreshAllowed(true);
								if(roomManager!=null)
									roomManager.LoadReservationRoom(reservationEntity.getReservationBox_LeftPanel().getParentReservation().getItemTimestampRsv());
							}					
						}
					}
				break;
			case ACTION_UP : 
					upDownCounter--;
					//Log.e("Odais", "action up");
					if(cameraMoved && (upDownCounter==0))
					{
						this.enginePreviousAction.setAction(Action.ACTION_UP);
						cameraMoved = false;
					}
					else if(upDownCounter==0)
					{
						if(this.engineAction.getSceneContext().getScene().getUserData()=="Room")
						{
							if(this.entity == null)
							{
								if(cameraMoved == false)
								{
									//create a new character, carrying a reservation
									//TODO fetch reservation from list
//									Reservation reservation = new Reservation("El Tchitcho", "Buggy", "3", "1000", "1554E1", "0", "120612", "1900");
//									People character = new People(this.engineAction.getSceneContext(), this.engineAction.getPosition().getPosX(), this.engineAction.getPosition().getPosY());
//									character.setReservation(reservation);
//									ArrayList<People> tmpList = (ArrayList<People>) this.enginePopulation.getList("People");
//									tmpList.add(character);
								}
							}
							else if(this.entity.getUserData()==EnginePopulation.ENGINE_POPULATION_TABLE)
							{
								Table tmpTable = (Table) this.entity;
								tmpTable.expandReservationList();
							}
							else if(this.entity.getUserData()==EnginePopulation.ENGINE_POPULATION_RESERVATIONENTITY)
							{
								reservationEntity = (ReservationEntity) this.entity;
								reservationEntity.getReservationBox_RightPanel().setGrabbed(false);
								reservationEntity.getReservationBox_RightPanel().setVisible(false);
								reservationEntity.getReservationBox_RightPanel().animate(this.engineAction.getSceneContext(), 0, 0, reservationEntity.getReservationBox_RightPanel().getX(), reservationEntity.getReservationBox_RightPanel().getY(), 1/this.roomCamera.getZoomFactor(), 1, 1);
								roomManager.setStatus(reservationEntity.getReservationBox_RightPanel().getParentReservation(), DataManager.RESERVATION_STATUS_CONFIRMED);
								roomManager.RefreshList();
							}
						}
						else
						{
							if(this.entity == null)
							{
								
							}
							else if(this.entity.getUserData()==EnginePopulation.ENGINE_POPULATION_RESERVATIONENTITY)
							{
								reservationEntity = (ReservationEntity) this.entity;
								reservationEntity.getReservationBox_LeftPanel().setGrabbed(false);
								reservationEntity.getReservationBox_LeftPanel().setVisible(true);

								if(reservationListModifierManager!=null)
									reservationListModifierManager.setmRefreshAllowed(true);
								//reservationBox.animate(this.engineAction.getSceneContext(), reservationBox.getX(), reservationBox.getY(), this.roomCamera.getZoomFactor(), 1, 1);
							}
						}

						this.entity = null;
					}
					
					entityMoved = false;
					
				break;
				
			case ACTION_MOVE :  				//For entities
				//Log.e("Odais", "action move");					
				
				if(this.entity != null)
				{
//					if(this.entity.getUserData()=="People")
//					{
//						this.entity.setPosition(this.engineAction.getPosition().getPosX(), this.engineAction.getPosition().getPosY());
//						entityMoved = true;
//					}
//					else 
					if(this.entity.getUserData()==EnginePopulation.ENGINE_POPULATION_RESERVATIONENTITY)
					{
						//this.entity.setPosition(this.engineAction.getPosition().getPosX(), this.engineAction.getPosition().getPosY());
						//When moving, both are visible... when dropped, we chose which will be visible and which will not, depending on the scene
						reservationEntity = (ReservationEntity) this.entity;
						reservationEntity.getReservationBox_LeftPanel().setVisible(true);
						reservationEntity.getReservationBox_RightPanel().setVisible(true);
						
						//if coordinates come from second scene, we must apply offset and scale factor
						if(this.engineAction.getSceneContext().getScene().getUserData()=="Room")
						{
							scaledX = (this.engineAction.getPosition().getPosX() - this.roomCamera.getXMin()) * this.roomCamera.getZoomFactor() + SCENE_OFFSET;
							scaledY = (this.engineAction.getPosition().getPosY() - this.roomCamera.getYMin()) * this.roomCamera.getZoomFactor();
						}
						else
						{
							scaledX = this.engineAction.getPosition().getPosX();
							scaledY = this.engineAction.getPosition().getPosY();
						}

						if(this.engineAction.getSceneContext().getScene().getUserData()=="Room")
						{
							scaledXSceneB = this.engineAction.getPosition().getPosX();
							scaledYSceneB = this.engineAction.getPosition().getPosY();
						}
						else
						{
							reservationEntity.getReservationBox_RightPanel().setScale(1/this.roomCamera.getZoomFactor());
							scaledXSceneB = (this.engineAction.getPosition().getPosX()-SCENE_OFFSET)/this.roomCamera.getZoomFactor() + this.roomCamera.getXMin();
							scaledYSceneB = (this.engineAction.getPosition().getPosY()/this.roomCamera.getZoomFactor()) + this.roomCamera.getYMin();
						}
						
						reservationEntity.getReservationBox_LeftPanel().setPosition(scaledX, scaledY);
						reservationEntity.getReservationBox_RightPanel().setPosition(scaledXSceneB, scaledYSceneB);
						entityMoved = true;
					}
				}

				//Log.e("Odais", "action move " + this.roomCamera.getYMin() + " " + this.roomCamera.getCenterY());	
				break;
				
			case ACTION_SCROLL : 				//For camera
				//Log.e("Odais", "action scroll");
					if(this.entity == null)
					{
						if(this.engineAction.getSceneContext().getScene().getUserData()=="Room")
						{
							final float zoomFactor = this.roomCamera.getZoomFactor();
							this.roomCamera.offsetCenter(-(this.engineAction.getPosition().getPosX() / zoomFactor), this.engineAction.getPosition().getPosY() / zoomFactor);
							
							
							cameraMoved = true;

							this.enginePreviousAction.setAction(Action.ACTION_SCROLL);
						}
					}
				break;
			case ACTION_ZOOM : 
				//Log.e("Odais", "action zoom");

				if(this.entity == null | upDownCounter==2)
				{
					cameraMoved = true;
					this.roomCamera.setZoomFactor(this.engineAction.getZoomFactor());
					this.enginePreviousAction.setAction(Action.ACTION_ZOOM);
				}
				break;
			default:
				break;
		}
	
	}

}